#!/usr/bin/env python3
import nmap

def scan_nmap_A(network_prefix):
    nm = nmap.PortScanner()#实例化PortScanner方法
    scan_result = nm.scan(hosts=network_prefix,arguments='-v -n -A')
    #-v 详细信息
    #-n 去掉dns解析
    #-A 扫描综合信息
    #print(scan_result) 利用 for 一层层剥离，查看
    #for k,v in scan_result.items():
    #    print(k,v)
    #    print("*"*20)
    for host,result in scan_result['scan'].items():
        if result['status']['state'] == 'up':
            print("#"*15 + "主机IP：" + host + "#"*15)
            for os in result['osmatch']:#操作系统信息字段内容
                print("操作系统为：" + os['name'] + "  准确度为："+ os["accuracy"])
        id_n = 1
        try:
            for port in result["tcp"]:
                try:
                    print("="*15 + "TCP服务信息"+'['+str(id_n)+']'+"="*15)
                    id_n += 1
                    print("TCP:端口号" + str(port))
                    try:
                        print("状态："+result["tcp"][port]["state"])
                    except:
                        pass

                    try:
                        print("原因："+result["tcp"][port]["reason"])
                    except:
                        pass


                    try:
                        print("额外信息："+result["tcp"][port]["extrainfo"])
                    except:
                        pass

                    try:
                        print("名字："+result["tcp"][port]["name"])
                    except:
                        pass
                    try:
                        print("版本："+result["tcp"][port]["version"])
                    except:
                        pass
                    try:
                        print("产品："+result["tcp"][port]["product"])
                    except:
                        pass
                    try:
                        print("CPE："+result["tcp"][port]["cpe"])
                    except:
                        pass
                    try:
                        print("脚本："+result["tcp"][port]["script"])
                    except:
                        pass
                except:
                    pass
        except:
            pass
                
        try: 
            for port in result["udp"]:
                try:
                    print("="*15 + "UDP服务信息"+'['+str(id_n)+']'+"="*15)
                    id_n += 1
                    print("UDP:端口号" + str(port))
                    try:
                        print("状态："+result["tcp"][port]["state"])
                    except:
                        pass

                    try:
                        print("原因："+result["tcp"][port]["reason"])
                    except:
                        pass


                    try:
                        print("额外信息："+result["tcp"][port]["extrainfo"])
                    except:
                        pass

                    try:
                        print("名字："+result["tcp"][port]["name"])
                    except:
                        pass
                    try:
                        print("版本："+result["tcp"][port]["version"])
                    except:
                        pass
                    try:
                        print("产品："+result["tcp"][port]["product"])
                    except:
                        pass
                    try:
                        print("CPE："+result["tcp"][port]["cpe"])
                    except:
                        pass
                    try:
                        print("脚本："+result["tcp"][port]["script"])
                    except:
                        pass
                except:
                    pass
        except:
            pass

        print("="*20 + "地址详细信息" + "="*20)
        try:
            print("IP地址：" + result["addresses"]["ipv4"])
            print("MAC地址：" + result["addresses"]["mac"])
        except:
            pass


if __name__ == "__main__":
    import sys
    scan_nmap_A(sys.argv[1])
